package com.shopping.orm;

import java.util.Date;
import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.shopping.orm.User;

public class UserDao extends HibernateDaoSupport{
	
	
	private static final Log log = LogFactory.getLog(UserDao.class);
		
	public void save(User user){
		log.debug("saving user instance");
		try{
		getHibernateTemplate().save(user);
		log.debug("save successful");
		}catch(RuntimeException re){
			log.error("save failed",re);
			throw re;
		}
	}
	
	public void delete(User user) {
        log.debug("deleting User instance");
        try {
            getHibernateTemplate().delete(user);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
	
	public void update(User user){
		log.debug("Upate User instance");
		try{
			getHibernateTemplate().saveOrUpdate(user);
			log.debug("update successful");
			
		}catch(RuntimeException re){
			log.error("update failed",re);
			throw re;
		}
	}
	public List findByExample(User instance){
		log.debug("get User instance with +:example");
		try{
			List result = getHibernateTemplate().findByExample(instance);
			log.debug("get successful!");
			return result;
		}catch(RuntimeException re){
			log.error("get failed",re);
			throw re;
		}
	}
	
	public List findByproperty(String propertyName,Object value){
		log.debug("finding User instance with property:"+ propertyName + ",value:"+value);
		try {
		    String queryString = "from User as model where model." 
		         						+ propertyName + "= ?";
		    return getHibernateTemplate().find(queryString, value);
		}catch(RuntimeException re){
			log.error("find by property failed",re);
			throw re;
		}
	}
	
	
	public List findAll(){
		log.debug("get User instance");		
		try{
			List result = getHibernateTemplate().findByExample("User");
			log.debug("find successful!");
			return result;
		}catch(RuntimeException re){
			log.error("get failed",re);
			throw re;
		}
	}
	
	
	public User findById(String uid){
		log.debug("get User instance with id:+uid");
		try{
			User instance = (User)getHibernateTemplate().get("User", uid);
			log.debug("get successful!");
			return instance;
		}catch(RuntimeException re){
			log.error("get failed",re);
			throw re;
		}	
	}
	
}
